package com.contract.spirit.ui.order.fragment

import android.os.Bundle
import com.contract.commonlib.base.BaseFragment
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.refresh.IRefreshCallBack
import com.contract.commonlib.refresh.RefreshManager
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.config.Constant
import com.contract.spirit.databinding.FrgOrderBinding
import com.contract.spirit.ui.order.OrderViewModel
import com.contract.spirit.ui.order.adapter.GuarantingOrderAdapter

/**
 * @author: zhengjun
 * created: 2021/9/28
 * desc:
 */
class OrderGuarantingFrg : BaseFragment<FrgOrderBinding, OrderViewModel>() {
    lateinit var exchangeType: String
    private val orderAdapter by lazy {
        GuarantingOrderAdapter()
    }

    companion object {
        fun newsInstance(exchangeType: String): OrderGuarantingFrg {
            val taskListFragment = OrderGuarantingFrg()
            val bundle = Bundle()
            bundle.putString(Constant.EXCHANGE_TYPE, exchangeType)
            taskListFragment.arguments = bundle
            return taskListFragment
        }
    }

    override fun setContentView(): Int {
        return R.layout.frg_order
    }

    override fun initData() {
        exchangeType = arguments?.getString(Constant.EXCHANGE_TYPE).toString()
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvOrder, mContext)
        footView.setCustomBackGroudnColor(R.color.bgGrayColor)
        orderAdapter.setFooterView(footView)
        mBinding.rvOrder.adapter = orderAdapter
        getOrderList()
    }

    override fun onReload() {
        mViewModel.guarantOrderListRes.value = null
        getOrderList()
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.guarantOrderListRes.observe(this, { it ->
            parseState(it, {
                orderAdapter.setList(it)
                setDataLoadFinish(it.size, mBinding.refreshLayout)
            }, {
                endRefreshing(mBinding.refreshLayout)
            })
        })
    }

    override fun initRefresh() {
        RefreshManager.getInstance().initRefresh(
            mContext,
            footView,
            false,
            mBinding.refreshLayout,
            object : IRefreshCallBack {
                override fun onRefresh() {
                    getOrderList()
                }
            })
    }

    private fun getOrderList() {
        mViewModel.getGuarantOrderList(exchangeType)
    }
}